package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.Result;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author 董万均
 * @since 2020-10-25
 */
@RestController
@RequestMapping("/eduservice/video")
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private VodClient vodClient;

    //添加小结
    @PostMapping("addVideo")
    public Result addVideo(@RequestBody EduVideo eduVideo){
        videoService.save(eduVideo);
        return Result.ok();
    }

    //查询出当前小结
    @GetMapping("getVideo/{id}")
    public Result getVideo(@PathVariable String id){
        EduVideo video = videoService.getById(id);
        return Result.ok().data("video",video);
    }
    //修改当前小结
    @PostMapping("updateVideo")
    public Result updateVideo(@RequestBody EduVideo eduVideo){
        videoService.updateById(eduVideo);
        return Result.ok();
    }
    //删除当前小结，这只是当前删除方法，小结中还有视频，删除小结的同时还有删除视频
    @DeleteMapping("deleteVideo/{id}")
    public Result deleteVideo(@PathVariable() String id){
        //先删除视频,该id是小结id，先获取到视频id
        EduVideo eduVideo = videoService.getById(id);
        String videoId = eduVideo.getVideoSourceId();
        //判断视频id是否为null
        if (!StringUtils.isEmpty(videoId)){
            Result result = vodClient.removeVideo(videoId);
            if (result.getCode()==20001){
                throw new GuliException(20001,"删除单个视频，微服务宕机，使用熔断器...");
            }
        }
        //删除小结
        videoService.removeById(id);
        return Result.ok();
    }
}

